package top.xierongsheng.code;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.lang.Nullable;

@Data
@Schema(name = "统一返回对象格式")
public class BaseVo<T> {

    @Schema(description = "处理状态码")
    private int code;

    @Schema(description = "返回数据对象")
    private T data;

    @Schema(description = "处理消息")
    private String message;

    public BaseVo(int code, @Nullable T data, String message) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public static <T> BaseVo<T> of(int code, @Nullable T data, String message) {
        return new BaseVo<>(code, data, message);
    }

    public static <T> BaseVo<T> ok(@Nullable T data) {
        return of(State.ok.code, data, State.ok.message);
    }

    public static <T> BaseVo<T> fail(@Nullable T data) {
        return of(State.fail.code, data, State.fail.message);
    }

    public static <T> BaseVo<T> error(@Nullable T data) {
        return of(State.error.code, data, State.error.message);
    }

    public static <T> BaseVo<T> authentication(@Nullable T data) {
        return of(State.authentication.code, data, State.authentication.message);
    }

    public static <T> BaseVo<T> authority(@Nullable T data) {
        return of(State.authority.code, data, State.authority.message);
    }

    public BaseVo<T> message(String message){
        this.message = message;
        return this;
    }


    //状态枚举
    private enum State {
        ok(0, "操作成功"), fail(1, "操作失败"), error(500, "服务器异常,请销后重试"),
        authentication(100, "token已过期"), authority(300, "您没有此权限");

        State(Integer code, String message) {
            this.code = code;
            this.message = message;
        }

        private final Integer code;
        private final String message;

    }
}
